﻿using AutoMapper;
using BasicData.Application.Input;
using BasicData.Application.Input.Workshop;
using BasicData.Application.Output;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Repositories;
using BasicData.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;


namespace BasicData.Application.Services.Workshop
{
    public class WorkshopServices : IWorkshopServices
    {
        IBasicModelRepository<WorkshopModel> _repository;
        IMapper mapper;

        public WorkshopServices(IBasicModelRepository<WorkshopModel> repository, IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
        }

        public ApiResult<int> AddWorkshop(WorkshopInput input)
        {
           var workshop =mapper.Map<WorkshopModel>(input);
            workshop.Id=YitIdHelper.NextId();
            workshop.CreatedAt = DateTime.Now;
            workshop.CreatedBy = "wyj";
            _repository.Create(workshop);
            return new ApiResult<int>()
            {
               Code = ApiEnums.OK,
               Message = "添加成功",
               Data=1
            };
        }

        public ApiResult<int> DeleteWorkshop(long id)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = _repository.GetValue().FirstOrDefault(x => x.Id == id);
            if (model==null)
            {
                res.Code = ApiEnums.Fail;
                res.Message = "未找到该数据";
                return res;
            }
            model.IsDeleted = true;
            int i=_repository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Error;
                res.Message = "删除失败";
                res.Data = i;
            }
            return res;
        }

        public ApiPaging<WorkshopOutput> GetWorkshop(PageInput input)
        {
            ApiPaging<WorkshopOutput> res = new ApiPaging<WorkshopOutput>();
            var workshop = _repository.GetValue().Where(x => x.IsDeleted == false);
            
            var query=workshop.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
            var list=mapper.Map<List<WorkshopOutput>>(query);
            return new ApiPaging<WorkshopOutput>()
            {
                PageDatas = list,
                TotalCount = workshop.Count(),
                Code = ApiEnums.OK,
                Message = "获取成功",
               
            };
        }

        public ApiResult<List<WorkshopOutput>> ShowWorkshop()
        {
            var model = _repository.GetValue();
            return new ApiResult<List<WorkshopOutput>>()
            {
                Code = ApiEnums.OK,
                Message = "获取成功",
                Data = mapper.Map<List<WorkshopOutput>>(model)
            };
        }

        public ApiResult<int> UpdateWorkshop(UpdateWorkshopInput input)
        {
           var modedl=mapper.Map<WorkshopModel>(input);
            modedl.UpdatedAt = DateTime.Now;
            modedl.CreatedBy = "wyj";
            _repository.Update(modedl);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "修改成功",
                
            };
        }
    }
}
